home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Python 1.3 / Python 1.3 68K / scripts / PackLibDir.py < prev    next >
Encoding:
Python Source  |  1995-10-11  |  2.3 KB  |  90 lines  |  [TEXT/PYTH]

  1. #
  2. # Turn a pyc file into a resource file containing it in 'PYC ' resource form
  3. import addpack
  4. addpack.addpack('Tools')
  5. addpack.addpack('bgen')
  6. addpack.addpack('res')
  7. from Res import *
  8. import Res
  9. from Resources import *
  10. import os
  11. import macfs
  12. import sys
  13.  
  14. READ = 1
  15. WRITE = 2
  16. smAllScripts = -3
  17.  
  18. error = 'mkpycresourcefile.error'
  19.  
  20. def Pstring(str):
  21.     if len(str) > 255:
  22.         raise ValueError, 'String too large'
  23.     return chr(len(str))+str
  24.     
  25. def createoutput(dst):
  26.     """Create output file. Return handle and first id to use."""
  27.     
  28.  
  29.     FSpCreateResFile(dst, 'PYTH', 'rsrc', smAllScripts)
  30.     output = FSpOpenResFile(dst, WRITE)
  31.     UseResFile(output)
  32.     num = 128
  33.     return output, num
  34.     
  35. def writemodule(name, id, data):
  36.     """Write pyc code to a PYC resource with given name and id."""
  37.     # XXXX Check that it doesn't exist
  38.     res = Resource(data)
  39.     res.AddResource('PYC ', id, name)
  40.     res.WriteResource()
  41.     res.ReleaseResource()
  42.         
  43. def mkpycresourcefile(src, dst):
  44.     """Copy pyc file/dir src to resource file dst."""
  45.     
  46.     if not os.path.isdir(src) and src[-4:] <> '.pyc':
  47.             raise error, 'I can only handle .pyc files or directories'
  48.     handle, oid = createoutput(dst)
  49.     if os.path.isdir(src):
  50.         id = handlesubdir(handle, oid, src)
  51.     else:
  52.         id = handleonepycfile(handle, oid, src)
  53.     print 'Wrote',id-oid,'PYC resources to', dst
  54.     CloseResFile(handle)
  55.             
  56. def handleonepycfile(handle, id, file):
  57.     """Copy one pyc file to the open resource file"""
  58.     d, name = os.path.split(file)
  59.     name = name[:-4]
  60.     print '  module', name
  61.     writemodule(name, id, open(file, 'rb').read())
  62.     return id+1
  63.     
  64. def handlesubdir(handle, id, srcdir):
  65.     """Recursively scan a directory for pyc files and copy to resources"""
  66.     print 'Directory', srcdir
  67.     src = os.listdir(srcdir)
  68.     for file in src:
  69.         file = os.path.join(srcdir, file)
  70.         if os.path.isdir(file):
  71.             id = handlesubdir(handle, id, file)
  72.         elif file[-4:] == '.pyc':
  73.             id = handleonepycfile(handle, id, file)
  74.     return id
  75.                 
  76.     
  77. if __name__ == '__main__':
  78.     args = sys.argv[1:]
  79.     if not args:
  80.         ifss, ok = macfs.GetDirectory('Select root of tree to pack:')
  81.         if not ok:
  82.             sys.exit(0)
  83.         args = [ifss.as_pathname()]
  84.     for ifn in args:
  85.         ofss, ok = macfs.StandardPutFile('Output for '+os.path.split(ifn)[1])
  86.         if not ok:
  87.             sys.exit(0)
  88.         mkpycresourcefile(ifn, ofss.as_pathname())
  89.     sys.exit(1)            # So we can see something...
  90.